/*
    Copyright  Louis CHEREL cherel.louis@gmail.com  
    This file is part of Agenda de l'Efrei.

    Agenda de l'Efrei is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Agenda de l'Efrei is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Agenda de l'Efrei.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.efrei.AgendaEfrei;

import android.app.*;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class CheckNotes extends IntentService {
    
    private static boolean isAlertNeeded=false;
    private static boolean isNotesUpdaterLaunched=false;
    
    public CheckNotes(){
        super("CheckNotes");
    }
    
    @Override
    protected void onHandleIntent(Intent arg0) {
        Log.v("CheckNotes", "loaded");
        new NotesUpdater(this).execute();
        while(getIsNotesUpdaterLaunched()){}
        Log.v("CheckNotes", "Start execution "+isAlertNeeded);
        if(getIsAlertNeeded()){
            sendNotification("Nouvelle note");
        }
        else{
            Log.v("CheckNotes", "Pas de nouvelle note");
            //sendNotification("Pas de nouvelle note");
        }
    }
    
    public void sendNotification(String incomingMess){
        Intent resultIntent = new Intent(this, ViewNotes.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack
        stackBuilder.addParentStack(ViewNotes.class);
        // Adds the Intent to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        // Gets a PendingIntent containing the entire back stack
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        Bitmap mDecodeResource = BitmapFactory.decodeResource(getResources(), R.drawable.logi_notification);
        
        Notification.Builder mBuilder = new Notification.Builder(this)
                .setLargeIcon(mDecodeResource)
                .setSmallIcon(R.drawable.logi_notification_small)
                .setContentTitle(incomingMess)
                .setContentText("Cliquez pour en voir plus")
                .setContentIntent(resultPendingIntent)
                .setAutoCancel(true);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
    
    public static void setIsAlertNeeded(boolean b){
        isAlertNeeded=b;
    }
    
    public static boolean getIsAlertNeeded(){
        return isAlertNeeded;
    }
    
    public static void setIsNotesUpdaterLaunched(boolean in){
        isNotesUpdaterLaunched=in;
    }
    
    public static boolean getIsNotesUpdaterLaunched(){
        return isNotesUpdaterLaunched;
    }
}
